#include "mencoder.h"

#include "encoderoption.h"
#include "settingwidget.h"


#include "codecs/x264codec.h"

static EncoderInterface::Infomation my_info;

extern "C" EncoderInterface * create_instance(const QString &path)
{ return new Mencoder(path); }

Mencoder::Mencoder(const QString &path)
    :m_path(path)
{
    setupInfo();
}



EncoderInterface::Infomation Mencoder::infomation() const {
    return my_info;
}


void Mencoder::setupInfo() {
    my_info.author = "simophin";
    my_info.displayName = QObject::tr("mencoder");
    my_info.intro = QObject::tr("A high-perfomance multimedia encoder/decoder");
    my_info.name = "mencoder";
    my_info.site = "blog.simophin.net";

    EncoderCodec *codec;

    // Setup video codecs
    codec = new X264Codec(this);
    m_videoCodecs.insert(codec->name(),codec);
}

EncoderInterface::TaskId Mencoder::startConvert(EncoderOption *, ConvertOption *, int *) {
    return 0;
}

bool Mencoder::pauseConvert(TaskId) {
    return true;
}

bool Mencoder::cancelConvert(TaskId) {
    return true;
}

SettingWidgetInterface * Mencoder::configWidget(EncoderOption * eops, ConvertOption *) {
    SettingWidgetInterface *ret = NULL;
    if (!m_configWidgets.contains(eops->id())) {
	ret = new SettingWidget(eops,this);
	m_configWidgets.insert(eops->id(),ret);
	connect(ret,SIGNAL(destroyed(QObject *)),this,SLOT(onConfigWidgetDestroyed(QObject*)));
	return ret;
    }else{
	return m_configWidgets.value(eops->id(),NULL);
    }
    return NULL;
}


QList<EncoderCodec *> Mencoder::supportedAudioCodecs() {
    return m_audioCodecs.values();
}

 QList<EncoderCodec *> Mencoder::supportedVideoCodecs() {
    return m_videoCodecs.values();
}

int Mencoder::testConversionAvailable(const QString &) {
    return CAP_AUDIO | CAP_VIDEO | CAP_SUBTITLE;
}

QPixmap Mencoder::capturePreview(const QString &, int ) {
    return QPixmap();
}

void Mencoder::onConfigWidgetDestroyed(QObject * obj) {
    SettingWidget *w = qobject_cast<SettingWidget *>(obj);
    if (!w) return;

    QString key = w->encoderOption()->id();
    if (!m_configWidgets.contains(key)) return;

    m_configWidgets.remove(key);
}
